using UnityEditor;
using UnityEngine;

namespace Editor.Scripts
{
    public class CloudsResourceGeneratorUI : MonoBehaviour
    {
        [CustomEditor(typeof(CloudsResourceGenerator), true)]
        public class CloudsResourceGeneratorEditor : UnityEditor.Editor
        {
            private CloudsResourceGenerator _generator;
            private SerializedProperty _cloudsFolder;
            private SerializedProperty _noiseFolder;
            private SerializedProperty _texToResFld;
            private SerializedProperty _texSaveIndividual;
            // private bool _cloudsResFoldout;

            private void OnEnable()
            {
                _generator = (CloudsResourceGenerator)target;
                _cloudsFolder = serializedObject.FindProperty("cloudsFolder");
                _noiseFolder = serializedObject.FindProperty("noiseFolder");
                _texToResFld = serializedObject.FindProperty("texToResFld");
                _texSaveIndividual = serializedObject.FindProperty("texSaveIndividual");
            }

            public override void OnInspectorGUI()
            {
                base.OnInspectorGUI();
                serializedObject.Update();
                
                EditorGUILayout.BeginVertical(EditorStyles.helpBox);
                // _cloudsResFoldout = Foldout(_cloudsResFoldout, "体积云资源处理");
                // if (_cloudsResFoldout)
                // {
                    EditorGUI.indentLevel++;
                    CustomGUI();
                    EditorGUI.indentLevel--;
                // }
                EditorGUILayout.EndVertical();

                serializedObject.ApplyModifiedProperties();
            }

            private void CustomGUI()
            {
                _texToResFld.boolValue = EditorGUILayout.Toggle("体积云资源存储至Resource", _texToResFld.boolValue);
                _cloudsFolder.stringValue = _texToResFld.boolValue ? "Assets/Resources/Clouds" : EditorGUILayout.TextField("体积云资源路径：", _cloudsFolder.stringValue);
                
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                _texSaveIndividual.boolValue = EditorGUILayout.Toggle("体积云生成到独立文件夹", _texSaveIndividual.boolValue);
                if (GUILayout.Button("遍历生成Index与Data贴图")) _generator.GenerateClouds();
                if (GUILayout.Button("清空体积云数据")) _generator.ClearCloudsGeneration();
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                _noiseFolder.stringValue = EditorGUILayout.TextField("噪声资源路径：", _noiseFolder.stringValue);
                if (GUILayout.Button("生成Noise贴图")) _generator.GenerateNoise();
                EditorGUILayout.EndHorizontal();
                
                EditorGUILayout.BeginHorizontal(EditorStyles.helpBox);
                if (GUILayout.Button("生成Volume数据")) _generator.ApplyVolume();
                EditorGUILayout.EndHorizontal();

            }
            
            static bool Foldout(bool display, string title)
            {
                var style = new GUIStyle("ShurikenModuleTitle")
                {
                    font = new GUIStyle(EditorStyles.boldLabel).font,
                    border = new RectOffset(15, 7, 4, 4),
                    fixedHeight = 22,
                    contentOffset = new Vector2(20f, -2f),
                    fontSize = 11,
                    normal =
                    {
                        textColor = new Color(0.7f, 0.8f, 0.9f)
                    }
                };

                var rect = GUILayoutUtility.GetRect(16f, 25f, style);
                GUI.Box(rect, title, style);

                var e = Event.current;
                var toggleRect = new Rect(rect.x + 4f, rect.y + 2f, 13f, 13f);
                switch (e.type)
                {
                    case EventType.Repaint:
                        EditorStyles.foldout.Draw(toggleRect, false, false, display, false);
                        break;
                    case EventType.MouseDown when rect.Contains(e.mousePosition):
                        display = !display;
                        e.Use();
                        break;
                }

                return display;
            }
        }
    }
}